package com.example.wisdomclassroomapi.application.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.wisdomclassroomapi.common.enums.ExceptionInfoEnum;
import com.example.wisdomclassroomapi.common.exception.ApplicationException;
import com.example.wisdomclassroomapi.common.util.IdentificationUtil;
import com.example.wisdomclassroomapi.domain.entity.AuthUrBindEntity;
import com.example.wisdomclassroomapi.domain.entity.AuthUserEntity;
import com.example.wisdomclassroomapi.domain.entity.GradeUserBindEntity;
import com.example.wisdomclassroomapi.domain.service.AuthUrBindDomainService;
import com.example.wisdomclassroomapi.domain.service.AuthUserDomainService;
import com.example.wisdomclassroomapi.domain.service.GradeUserBindDomainService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Package: com.example.wisdomclassroomapi.application.service
 * @Description: <>
 * @Author: scholes
 * @CreateDate: 2022-04-18
 * @UpdateUser: scholes
 * @UpdateDate: 2022-04-18
 * @UpdateRemark: <>
 * @Version: 1.0
 */
@Service
public class AuthUserApplicationService {
    @Resource
    private AuthUserDomainService authUserDomainService;
    @Resource
    private AuthUrBindDomainService authUrBindDomainService;
    @Resource
    private GradeUserBindDomainService gradeUserBindDomainService;
    @Resource
    private PasswordEncoder passwordEncoder;

    public String add(Map<String, String> params) {
        AuthUserEntity entity = new AuthUserEntity();

        entity.setUsername(params.get("username"));
        List<AuthUserEntity> list = this.list(entity);
        if (!list.isEmpty()) throw new ApplicationException(ExceptionInfoEnum.AM_USER_USERNAME_EXIST);

        entity.setId(IdentificationUtil.shortUuid());
        entity.setPassword(passwordEncoder.encode(params.get("password")));
        entity.setEmail(params.get("email"));
        entity.setMobile(params.get("mobile"));
        entity.setRealName(params.get("realName"));

        authUserDomainService.add(entity);
        return entity.getId();
    }

    /**
     * 绑定角色
     * @param userId
     * @param roleIds
     */
    public void bindRole(String userId, List<String> roleIds) {
        AuthUrBindEntity entity = new AuthUrBindEntity();
        entity.setUserId(userId);

        // 删除所有绑定的角色
        List<AuthUrBindEntity> list = authUrBindDomainService.list(entity);
        list.forEach(authUrBindEntity -> {
            authUrBindDomainService.deleteById(authUrBindEntity.getId());
        });

        for (String roleId: roleIds) {
            entity.setId(IdentificationUtil.shortUuid());
            entity.setRoleId(roleId);
            authUrBindDomainService.add(entity);
        }
    }

    /**
     * 绑定班级
     * @param userId
     * @param classIds
     */
    public void bindClass(String userId, List<String> classIds) {
        GradeUserBindEntity entity = new GradeUserBindEntity();
        entity.setUserId(userId);

        // 删除所有绑定的班级
        List<GradeUserBindEntity> list = gradeUserBindDomainService.list(entity);
        list.forEach(gradeUserBindEntity -> {
            gradeUserBindDomainService.deleteById(gradeUserBindEntity.getId());
        });

        for (String classId: classIds) {
            entity.setId(IdentificationUtil.shortUuid());
            entity.setGradeId(classId);
            gradeUserBindDomainService.add(entity);
        }
    }



    public AuthUserEntity findById(String id) {
        return authUserDomainService.findById(id);
    }

    public int updateById(AuthUserEntity entity) {
        return authUserDomainService.updateById(entity);
    }

    public int deleteById(String id) {
        return authUserDomainService.deleteById(id);
    }

    public IPage<AuthUserEntity> page(AuthUserEntity entity, IPage page) {
        return authUserDomainService.page(entity, page);
    }

    public List<AuthUserEntity> list(AuthUserEntity entity) {
        return authUserDomainService.list(entity);
    }

}